import java.io.IOException;
import org.commonmark.node.*;
import org.commonmark.parser.Parser;
import org.commonmark.renderer.html.HtmlRenderer;
//import java.io.*;
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.concurrent.Worker;
import javafx.concurrent.Worker.State;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.ScrollPane;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;

public class MarkdownJavaDemo1 extends Application {
  @Override
  public void start(final Stage stage) {
    stage.setWidth(400);
    stage.setHeight(500);
    Scene scene = new Scene(new Group());


    final WebView browser = new WebView();
    final WebEngine webEngine = browser.getEngine();

    ScrollPane scrollPane = new ScrollPane();
    scrollPane.setContent(browser);

    webEngine.getLoadWorker().stateProperty()
        .addListener(new ChangeListener<State>() {
          @Override
          public void changed(ObservableValue ov, State oldState, State newState) {

            if (newState == Worker.State.SUCCEEDED) {
              stage.setTitle(webEngine.getLocation());
            }

          }
        });

    //解析Markdown，并加载到HTNL
    Parser parser = Parser.builder().build();
    Node document = parser.parse("This is *Sparta* **Notice:** $\\sum^2$");
    HtmlRenderer renderer = HtmlRenderer.builder().build();
    String str = renderer.render(document); // "<p>This is <em>Sparta</em></p>\n"
    webEngine.loadContent(str, "text/html");

    scene.setRoot(scrollPane);
    stage.setScene(scene);
    stage.show();
  }

  public static void main(String[] args) {
    launch(args);
  }

  /*
  public static void main(String[] args)  throws IOException {

    Parser parser = Parser.builder().build();
    Node document = parser.parse("This is *Sparta* **Notice:** $\\sum^2$");
    HtmlRenderer renderer = HtmlRenderer.builder().build();
    String str = renderer.render(document); // "<p>This is <em>Sparta</em></p>\n"
    FileWriter fw = new FileWriter("1.html");
    fw.write(str);
    fw.flush();
    fw.close();
  }
  */
}
